from pymysql.cursors import Cursor

from dao.BaseDao import BaseDao


class MySqlDao(BaseDao):
    def showTable(self, db: Cursor, dbName):
        sql = 'SELECT TABLE_NAME FROM information_schema.TABLES WHERE table_schema = %s '
        db.execute(sql, [dbName])
        dl = db.fetchall()
        tableList = []
        for db in dl:
            tableList.append(db['TABLE_NAME'])
        return tableList

    def showdbs(self, db: Cursor):
        db.execute("show databases")
        dl = db.fetchall()
        dbList = []
        for db in dl:
            dbList.append(db['Database'])
        return dbList

    def querry(self, db: Cursor, dbName, tableName):
        sql = "select * from %s.%s" % (dbName, tableName)
        db.execute(sql)
        dl = db.fetchall()
        return dl
